package com.lagou.rabbitmq.handler;

import com.lagou.rabbitmq.constants.RabbitConstants;
import com.lagou.rabbitmq.domain.Goods;
import com.lagou.rabbitmq.service.GoodsService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @className: StockHandler
 * @auther: Jerry
 * @date: 2021/3/18 9:30
 * @desctiption: TODO
 * @version: 1.0
 */
@Component
public class StockHandler {

    @Autowired
    private GoodsService goodsService;

    @RabbitListener(queues = RabbitConstants.STOCK_QUEUE)
    public void onMessage(StockMessage message) {
        System.out.println("StockHandler.handler...");
        Goods g = goodsService.getById(message.getGoodsId());
        Goods goods = new Goods();
        goods.setId(message.getGoodsId());
        switch (message.getAction()) {
            case StockMessage.ADD_STOCK:
                goods.setStock(g.getStock() + 1);
                break;
            case StockMessage.REDUCE_STOCK:
                goods.setStock(g.getStock() - 1);
                break;
            default:
                System.out.println("未知方式");
                break;
        }
        goods.setGmtModified(new Date());
        goodsService.updateById(goods);
    }
}
